package threading;
//懒汉模式实现单例模式
class SingletonLazy{
    //加 volatile 比较靠谱
    private static volatile SingletonLazy instance = null;
    private SingletonLazy(){};//构造方法设置为private
    public static SingletonLazy getInstance(){
        if(instance == null) {
            //判断是否要加锁，如果对象有了就不必加锁了，此时线程安全
            synchronized (SingletonLazy.class) {
                if (instance == null) {//是否要创建对象
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }

}
public class ThreadDemo16 {
    public static void main(String[] args) {
    SingletonLazy s1 = SingletonLazy.getInstance();
    SingletonLazy s2 = SingletonLazy.getInstance();
    System.out.println(s1 == s2);//同一个实例
    }
}
